package com.jsmile.cloud.msgcenter.web.mq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jsmile.cloud.authc.api.enums.SmsTypeEnum;
import com.jsmile.cloud.msgcenter.api.constants.MsgConstants;
import com.jsmile.cloud.msgcenter.api.to.SmsTo;
import com.jsmile.cloud.msgcenter.web.config.MsgConfig;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.cache.kit.CacheKit;
import com.jsmile.mall.sms.SmsFactory;
import com.jsmile.mall.sms.req.ReqSmsSend;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * rabbitmq消息配置
 *
 * @author 亮亮
 * @version 1.0.0
 * @date 2017年3月30日 下午3:34:16
 */
@Component
@Slf4j
@AllArgsConstructor
public class SmsSendListener {

    private final AmqpTemplate amqpTemplate;
    private final MsgConfig bizConfig;

    @RabbitListener(queuesToDeclare = @Queue(MsgConstants.SmsMq.SMS_SEND_QUEUE))
    public void receiveSms(SmsTo smsTo) {
        log.info("短信发送参数：{}", smsTo);
        ReqSmsSend smsSendTo = ReqSmsSend.builder().PhoneNumbers(smsTo.getPhone()).SignName(bizConfig.getSignNamesByTenantId(smsTo.getTenantId() + ""))
            .TemplateCode(bizConfig.getSmsTemplatesCode(smsTo.getTenantId() + "", smsTo.getSmsType())).TemplateParam(JSON.toJSONString(smsTo.getSmsParams())).build();
        SmsFactory.build().sendSms(smsSendTo);
        CacheKit.put(JSmileCacheEnum.oneMinute, SmsTypeEnum.getCacheName(smsTo.getSmsType()) + smsTo.getPhone() + "_tenantId_" + smsTo.getTenantId(),
            smsTo.getSmsParams().get("code"));// 注册短信缓存1分钟
    }
}
